<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: hnjy
  Date: 2025/2/22
  Time: 13:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>


        img {
            width: 50px;
            height: 50px;
        }
        body {
            font-family: Arial, sans-serif;
        }

        .pet-info {
            margin-bottom: 20px;
        }

        .comment-input {
            margin-top: 20px;
        }

        .comment-input textarea {
            width: 100%;
            height: 100px;
        }

        .comment-item {
            border: 1px solid #ccc;
            padding: 10px;
            margin-bottom: 10px;
        }

        .comment-item img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            vertical-align: middle;
        }

        .comment-item.info {
            display: inline-block;
            margin-left: 10px;
        }

        .comment-item.username {
            font-weight: bold;
        }

        .comment-item.time {
            color: #999;
        }

        .reply-btn {
            margin-top: 5px;
        }

        .reply-list {
            margin-left: 30px;
        }

        /* 弹窗遮罩样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        /* 弹窗内容样式 */
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            border-radius: 5px;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        }

        /* 关闭按钮样式 */
        .close {
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }
        .btn{
            background-color: #337AB7;
            color: white;
            border-radius: 3px;
            border: unset;
        }
    </style>
</head>
<body>
<h5 style="display: flex;align-items: center;justify-content: center; "><img src="${pageContext.request.contextPath}/statics/images/sun.png"/>
    <p style="font-size: 30px;">我叫${pet.petName}</p>
</h5>

<div style="width: 700px; margin: 0 auto;">
    <div style="float: left; margin: 0 auto;display: flex;">
        <div>
            <div style="width: 380px;display: flex;justify-content: center; align-items: center;flex-wrap: wrap">
                <div style="display: flex;width: 120px;margin: 10px;">
                    <div>
                        <img src="${pageContext.request.contextPath}/statics/images/number.png"/>

                    </div>
                    <div>
                        <div>编号</div>
                        <div style="margin-top: 10px;">${pet.id}</div>
                    </div>
                </div>

                <div style="display: flex;width: 120px;margin: 10px">
                    <div>
                        <img src="${pageContext.request.contextPath}/statics/images/birth.png"/>

                    </div>
                    <div>
                        <div>生日</div>
                        <div style="margin-top: 10px;font-size: 12px">${pet.birthday}</div>
                    </div>
                </div>

                <div style="display: flex;width: 120px;margin: 10px">
                    <div>
                        <img src="${pageContext.request.contextPath}/statics/images/type.png"/>

                    </div>
                    <div>
                        <div>品种</div>
                        <div style="margin-top: 10px;">${pet.petType}</div>
                    </div>
                </div>

                <div style="display: flex;width: 120px;margin: 10px">
                    <div>
                        <img src="${pageContext.request.contextPath}/statics/images/sex.png"/>
                    </div>
                    <div>
                        <div>性别</div>
                        <div style="margin-top: 10px;">${pet.sex}</div>
                    </div>
                </div>
            </div>

            <div style="display: flex;justify-content: flex-end;margin-top: 60px; margin-right: 60px;">

                <div onclick="location='${pageContext.request.contextPath}/sys/apply/toApply/${pet.id}'"
                        style="margin-left: 20px; border-radius: 2px; padding: 5px; background-color: #337AB7; width: 80px;height: 20px;text-align: center;color: white;">
                    想要领养
                </div>

                <div onclick="location='${pageContext.request.contextPath}/sys/home'"
                        style="margin-left: 20px;border-radius: 2px; padding: 5px; background-color: #337AB7; width: 80px;height: 20px;text-align: center;color: white;">
                    返回中心
                </div>

            </div>
        </div>

    </div>

    <div
            style="float: left;padding: 10px; width: 300px;height: 230px; background: url('${pageContext.request.contextPath}/statics/images/1.webp') no-repeat;background-size: 100% ;">
        <div style="text-indent: 2em;  padding-top: 20px;">${pet.remark}</div>
    </div>
</div>


<div style="clear: both;margin: 20p" ></div>

<div id="comment-list">
    <!-- 遍历顶级评论 -->
    <c:forEach items="${comments}" var="topComment">
        <div class="comment-item">
            <img src="${topComment.user.pic}" alt="头像">
            <div class="info">
                <span class="username">${topComment.user.username}</span>
                <span class="time">${topComment.commentTime}</span>
                <p>${topComment.content}</p>
            </div>
            <button class="btn" onclick="showDiv(${topComment.id})">回复</button>
            <!-- 遍历一级子评论 -->
            <c:forEach items="${topComment.replies}" var="firstLevelReply">
                <div class="comment-item" style="margin-left: 30px;">
                    <img src="${firstLevelReply.user.pic}" alt="头像">
                    <div class="info">
                        <span class="username">${firstLevelReply.user.username}</span>
                        <span class="time">${firstLevelReply.commentTime}</span>
                        <p>${firstLevelReply.content}</p>
                    </div>
                    <button class="btn" onclick="showDiv(${firstLevelReply.id})">回复</button>
                    <!-- 遍历二级子评论 -->
                    <c:forEach items="${firstLevelReply.replies}" var="secondLevelReply">
                        <div class="comment-item" style="margin-left: 60px;">
                            <img src="${secondLevelReply.user.pic}" alt="头像">
                            <div class="info">
                                <span class="username">${secondLevelReply.user.username}</span>
                                <span class="time">${secondLevelReply.commentTime}</span>
                                <p>${secondLevelReply.content}</p>
                            </div>
                        </div>
                    </c:forEach>
                </div>
            </c:forEach>
        </div>
    </c:forEach>
</div>

<div class="comment-input"  style="clear: both;margin-top: 20px">
    <form action="${pageContext.request.contextPath}/comments/add" method="post">
        <textarea name="content" placeholder="来说几句吧"></textarea>

        <input type="hidden" name="petId" value="${pet.id}"> <!-- 这里假设宠物ID为1，实际应动态获取 -->
        <input type="submit" value="评论" class="btn">
    </form>
</div>




<div id="modal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <div class="comment-input"  >
            <form action="${pageContext.request.contextPath}/comments/add" method="post">
                <input id="userId" type="hidden" name="parentCommentId" >
                <textarea name="content" placeholder="来说几句吧"></textarea>
                <input type="hidden" name="petId" value="${pet.id}"> <!-- 这里假设宠物ID为1，实际应动态获取 -->
                <input type="submit" value="回复" class="btn" style="background-color:#337AB7;color: white ">
            </form>
        </div>
    </div>
</div>

<script>
    function  showDiv(id) {
        // 获取按钮、弹窗和关闭按钮元素

        const modal = document.getElementById('modal');
        const closeBtn = document.querySelector('.close');
        document.getElementById("userId").value = id;

            modal.style.display = 'block';


        // 点击关闭按钮关闭弹窗
        closeBtn.addEventListener('click', () => {
            modal.style.display = 'none';
        });

        // 点击弹窗外部关闭弹窗
        window.addEventListener('click', (event) => {
            if (event.target === modal) {
                modal.style.display = 'none';
            }
        });
    }


    // 模拟提交评论，实际需要通过AJAX与后端交互
    document.querySelector('.comment-box button').addEventListener('click', function () {
        const commentContent = document.querySelector('.comment-box textarea').value;
        if (commentContent.trim() === '') {
            alert('请输入评论内容');
            return;
        }
        console.log('提交的评论内容:', commentContent);
        // 清空输入框
        document.querySelector('.comment-box textarea').value = '';
    });
</script>
</body>
</html>
